<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/layui/css/layui.css"/>
    <link href="/layui/css/modules/layer/default/layer.css" rel="stylesheet"/>
    <link rel="stylesheet" href="/css/index.css"/>
</head>
<body class="main-body-admin">
<nav style="margin-top: 10px;">
    <select id="collegeId" onchange="show()">
        <option value="DEFAULT"></option>
    </select>
    <button class="layui-btn" onclick="edit()">编辑负责人</button>
    <input class="layui-input-inline" id="searchValue"/>
    <button class="layui-btn layui-btn-normal" onclick="search()">搜索</button>
</nav>
<table id="major" lay-filter="test"></table>
<script src="/js/jquery.min.js"></script>
<script src="/js/config/server.js"></script>
<script src="/js/common/login.js"></script>
<script src="/js/common/upload.js"></script>
<script src="/layui/layui.all.js"></script>
<script>
  var table;
  var collegeId;

  function show() {
    collegeId = $("#collegeId").val();
    console.log(collegeId);
    if (collegeId !== 'DEFAULT') {
      layui.use('table', function () {
        table = layui.table;
        table.render({
          elem: '#major'
          , height: 515
          , url: host + '/rest/major/listByCollegeWithRefer/' + collegeId //数据接口
          , page: true //开启分页
          , limit: 15
          , limits: [15, 30, 45]
          , cols: [[ //表头
            {checkbox: true}
            , {field: 'majorId', title: 'ID', width: 160, sort: true}
            , {field: 'name', title: '专业名', width: 280}
            , {field: 'leader', title: '负责人', width: 280}
          ]]
        });
      });
    }
  }

  handlerGet('/rest/college/listTotal', 'admin', function (data) {
    data.data.forEach(function (item) {
      $("#collegeId").append('<option value="' + item.collegeId + '">' + item.name + '</option>');
    });
  },function (data) {
    layer.msg('加载学院失败，请检查网络'+data.responseText)
  });

  function reloadAll() {
    layui.use('table', function () {
      var table = layui.table;
      table.reload('major', {
        url: host + '/rest/major/listByCollegeWithRefer/' + collegeId
      });
    })
  }

  function updateValue(content, name, value) {
    return content.replace(name + '##VA', value)
  }

  function edit() {
    if ($("#collegeId").val() === 'DEFAULT') {
      layer.msg('请选择具体的专业')
    } else {
      var checkStatus = table.checkStatus('major');
      if (checkStatus.data.length !== 1) {
        layer.msg('请选择一条记录')
      }else{
        sessionStorage.setItem('leaderEditMajorId', checkStatus.data[0].majorId);
        frameTip('委派负责人', 'team/leader.html')
      }
    }
  }

  function search() {
    if ($("#collegeId").val() === 'DEFAULT') {
      layer.msg('请选择具体的专业')
    }else{
      var value = $("#searchValue").val();
      if (value === '' || value === ' ') {
        layer.msg('请输入查询参数');
        return;
      }
      layui.use('table', function () {
        var table = layui.table;
        table.reload('major', {
          url: host + '/rest/major/search/'+collegeId+'?name=' + value + ''
          , where: {}
        });
      })
    }
  }
  function frameTip(title, url) {
    layer.open({
      title: title
      , type: 2
      , content: url
      , btnAlign: 'c'
      , resize: true
      , tipsMore: true
      , area: ['270px', '300px']
      ,cancel: function () {
        sessionStorage.removeItem('leaderEditMajorId');
        sessionStorage.removeItem('editCollege');
        sessionStorage.removeItem('LeaderId');
        sessionStorage.removeItem('editMajor');
        reloadAll();
      }
    });
  }
</script>
<script id="editData" type="text/html">
    <div id="Tips">
        <label for="name">专业名</label>
        <input name="name" type="text" id="name" value="name##VA" required/>
    </div>
</script>
</body>
</html>